using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region LegalEntity

	/// <summary>
	/// LegalEntity object for NHibernate mapped table 'LegalEntity'.
	/// </summary>
	[Serializable]
	public class LegalEntity : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected string _legalEntityName;
		protected string _description;
		protected bool _isEnabled;
		protected IList _applications;
		protected IList _legalEntityEmployees;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public LegalEntity() { }

		public LegalEntity( string legalEntityName, string description, bool isEnabled )
		{
			this._legalEntityName = legalEntityName;
			this._description = description;
			this._isEnabled = isEnabled;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public string LegalEntityName
		{
			get { return _legalEntityName; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for LegalEntityName", value, value.ToString());
				_legalEntityName = value;
			}
		}

		public string Description
		{
			get { return _description; }
			set
			{
				if ( value != null && value.Length > 255)
					throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
				_description = value;
			}
		}

		public bool IsEnabled
		{
			get { return _isEnabled; }
			set { _isEnabled = value; }
		}

		public IList Applications
		{
			get
			{
				if (_applications==null)
				{
					_applications = new ArrayList();
				}
				return _applications;
			}
			set { _applications = value; }
		}

		public IList LegalEntityEmployees
		{
			get
			{
				if (_legalEntityEmployees==null)
				{
					_legalEntityEmployees = new ArrayList();
				}
				return _legalEntityEmployees;
			}
			set { _legalEntityEmployees = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is LegalEntity))
				throw new InvalidCastException("This object is not of type LegalEntity");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((LegalEntity)obj).Id);
					break;
				case "LegalEntityName":
						relativeValue = ( this.LegalEntityName != null ) ? this.LegalEntityName.CompareTo(((LegalEntity)obj).LegalEntityName) : -1;
					break;
				case "Description":
						relativeValue = ( this.Description != null ) ? this.Description.CompareTo(((LegalEntity)obj).Description) : -1;
					break;
				case "IsEnabled":
						relativeValue = ( this.IsEnabled != Convert.ToBoolean( null) ) ? this.IsEnabled.CompareTo(((LegalEntity)obj).IsEnabled) : -1;				
					break;
                default:
                    goto case "Id";
			}
            if (LegalEntity.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
